package pers.qianyu.month_202012.date_20201208;

import java.util.*;

/**
 * 349. 两个数组的交集
 * https://leetcode-cn.com/problems/intersection-of-two-arrays/
 *
 * @author mizzle rain
 * @date 2020-12-08 14:10
 */
public class Intersection {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> dict = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            dict.put(nums1[i], dict.getOrDefault(nums1[i], 0) + 1);
        }
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums2.length; i++) {
            if (dict.containsKey(nums2[i])) {
                set.add(nums2[i]);
            }
        }
        int[] arr = new int[set.size()];
        Iterator<Integer> iter = set.iterator();
        int i = 0;
        while (iter.hasNext()) {
            arr[i++] = iter.next();
        }
        return arr;
    }
}
